--
-- Author: shunguo.chen
-- Date: 2019-07-01 20:36:56
--
-- AvatarShopController
--

local AvatarShopView = import("..views.AvatarShopView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._model = app:getInst("AvatarShopModel")

    self._baseInfo = nil
end

function ClassRef:removeListeners()
    self.super.removeListeners(self)

end

function ClassRef:listEvent()
    return { }
end


function ClassRef:openView(data)
    if nil == self._view then
        self._view = AvatarShopView.new(app.mvc.VIEWTYPE_BLUR)
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
        self:addChild(self._view, data)

        local openBtn = app:sendMsg("IMRoomController", "getIMOpenBtn")
        if openBtn then
            self._view:addChild(openBtn, 2)
        end
    end

    app:sendMsg("IMSimpleController", "showView", 
        Enums.IMSimpleShowType.btn, nil, nil, SceneZOrder.systemUIZOrder)

    self._baseInfo = UD:getAvatarShopParams()
    self._view:refresh(self._model:getShopInfos(), self._baseInfo)
    self._view:refreshRedPoint()
    self._view:show()
end
function ClassRef:clearView()
    if self._view then
        self._view = nil
        self._model:clear()
    end

    app:sendMsg("IMSimpleController", "showView")
end

-------------------------------------------------------
function ClassRef:onHairCut()
    if not self:isViewAlive() then return end

    self._view:buyAvatarSuccess()
end

function ClassRef:onUpdateShopItem()
    if not self:isViewAlive() then return end

    self._view:refreshRedPoint()
    self._view:refreshShopPanel()
end

-------------------------------------------------------
function ClassRef:onOperate(op, data)
    -- print("AvatarShopController:onOperate op", op)
    if op == "buy_avatar" then
        -- 购买外显 data
        self._model:C2S_HairCut(data)
    elseif op == "im_visible" then
        -- 聊天按钮可见性
        self:_refreshIMVisible(data)
    elseif op == "get_resource" then
        -- 来源
        app:sendMsg("HandbookController", "openGotWayView", data)
    end
end

-- 刷新聊天相关可见性
function ClassRef:_refreshIMVisible(b)
    local imOpenBtn = self._view:getChildByName("IMOpenBtn")
    if imOpenBtn then
        imOpenBtn:setVisible(b)
    end
    if b then
        app:sendMsg("IMSimpleController", "showView", 
            Enums.IMSimpleShowType.btn, nil, nil, SceneZOrder.systemUIZOrder)
    else
        app:sendMsg("IMSimpleController", "hideView")
    end
end

return ClassRef
